package com.example.mpandroidchart1107;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("折线图");
        LineChart linechart =findViewById(R.id.linechart);// 实例化
        List<ILineDataSet> s=new ArrayList<>();//调用LineDataSet的方法
        /*
        *http://192.168.169.177:8889/ 老师网站地址
         */
        float[] f1 ={0.0f,1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f};
        float [] f2={9.0f,8.0f,7.0f,6.f,5.0f,4.0f,3.0f,2.0f,1.0f,0.0f};
        float [] f3={6.0f,6.0f,7.0f,4.0f,6.0f,5.0f,6.0f,6.0f,8.0f,6.0f,4.0f,6.0f,7.50f,5.0f,6.0f,6.0f,6.0f,6.0f,};

        LineDataSet demo1=getDataSet(f1,"上升");
        demo1.setColor(Color.parseColor("#00F5FF"));//线条颜色
        demo1.setDrawValues(false);//折线图不显示数值
        demo1.setDrawCircles(false);  //不显示圆点
        demo1.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);//线条平滑
        /*设置折线图填充*/
        demo1.setDrawFilled(false);/*是否填充*/
        LineData data = new LineData(demo1);/*要填充的线demo1*/
        s.add(demo1);

        LineDataSet demo2=getDataSet(f2,"下降");
        demo2.setColor(Color.parseColor("#00EE76"));//线条颜色
        demo2.setDrawCircles(false);  //不显示圆点
        demo2.setDrawValues(false);
        s.add(demo2);

        LineDataSet demo3=getDataSet(f3,"心电图 ");
        demo3.setColor(Color.parseColor("#FF0000"));//线条颜色红
        demo3.setLineWidth(2.0f); //线宽度
        demo3.setDrawCircles(false);  //不显示圆点
        demo3.setDrawValues(false);
        s.add(demo3);

        XAxis xAxis=linechart.getXAxis();//控制x轴
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawAxisLine(false);

        linechart.setData(new LineData(s));
        linechart.invalidate();

    }
    private  LineDataSet getDataSet(float[]f,String title){

        List<Entry> data =new ArrayList<>();//Entry相当于坐标值
        for (int x =0; x<f.length;x++) {//遍历浮点数数组
            Entry entry = new Entry(x, f[x]);//x坐标y 坐标
            data.add(entry);/*加入到点的集合中*/
        }
        return new LineDataSet(data,title);
    }
}





//    float [] t={9.0f,8.0f,7.0f,6.0f,5.0f,4.0f,3.0f,2.0f,1.0f};
//    List<Entry> date =new ArrayList<>();
//        for (int i=0 ; i <t.length; i++){
//        Entry entry =new Entry(i,t[i]);
//        data.add(entry);
//        }
//        LineDataSet demo1 =new LineDataSet(data,"demo");
//        List<ILineDataSet> i=new ArrayList<>();//调用LineDataSet的方法
//        i.add(demo1);
//        linechart.setData(new LineData(i));//线
//        linechart.invalidate();
// List<Entry> data =new ArrayList<>();//Entry相当于坐标值
//        for (int i=0 ; i <f1.length; i++){//遍历浮点数数组
//            Entry entry =new Entry(i,f1[i]); //x坐标y 坐标
//            data.add(entry);
//        }